import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.io.File;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Keyboard;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Players;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;
import org.rsbot.util.GlobalConfiguration.Paths;

@ScriptManifest(authors={"Insert Witty Name"}, name="Fxjewellry", version=0.1D, description="None")
public class Fxjewellry extends Script
  implements PaintListener
{
  private int expGained;
  private int expStart;
  private Fxjewellry.GUI gui;
  private Fxjewellry.UtilThread utilThread;
  private int ifaceID;
  private int levelGained;
  private int levelStart;
  private int mouseLo;
  private int mouseHi;
  private int numDone;
  private boolean running;
  private int secondaryID;
  private Fxjewellry.Smelter smelter;
  private Fxjewellry.STATES state;
  private final Fxjewellry.Timekeeper timekeeper;

  public Fxjewellry()
  {
    this.expGained = 0;
    this.expStart = 0;
    this.gui = null;
    this.utilThread = null;
    this.ifaceID = 34;
    this.levelGained = 0;
    this.levelStart = 0;
    this.mouseLo = 2;
    this.mouseHi = 3;
    this.numDone = 0;
    this.running = true;
    this.secondaryID = 1607;
    this.smelter = null;
    this.state = Fxjewellry.STATES.IDLE;
    this.timekeeper = new Fxjewellry.Timekeeper(this);
  }

  public Fxjewellry.STATES getState()
  {
    if (this.inventory.contains(2357)) {
      if ((!this.walking.isRunEnabled()) && (this.walking.getEnergy() > 50))
        return Fxjewellry.STATES.RUN_ENABLE;
      if (this.bank.isOpen())
        return Fxjewellry.STATES.BANK_CLOSE;
      if (this.interfaces.canContinue())
        return Fxjewellry.STATES.CLICK_CONTINUE;
      if (this.utilThread.getIdle() > 20L) {
        if (ifaceIsUp(this.interfaces.getComponent(446, 0)))
          return Fxjewellry.STATES.IFACE_NAVIGATE;
        if ((this.objects.getNearest(Fxjewellry.constants.furnace) != null) && (this.objects.getNearest(Fxjewellry.constants.furnace).isOnScreen())) if (this.smelter.getArea().contains(new RSTile[] { this.players.getMyPlayer().getLocation() }))
          {
            return Fxjewellry.STATES.CLICK_FURNACE;
          } if (!this.smelter.getArea().contains(new RSTile[] { this.players.getMyPlayer().getLocation() }))
        {
          return Fxjewellry.STATES.WALK_FURNACE;
        }
      }
    } else {
      if (this.bank.isOpen()) {
        if (this.inventory.getCount() > 1)
          return Fxjewellry.STATES.DEPOSIT;
        if (this.bank.getCount(new int[] { 2357 }) <= 0) {
          return Fxjewellry.STATES.RAGE_QUIT;
        }
        return Fxjewellry.STATES.WITHDRAW;
      }
      if (this.smelter.getBankArea().contains(new RSTile[] { this.players.getMyPlayer().getLocation() }))
      {
        return Fxjewellry.STATES.BANK_OPEN;
      }return Fxjewellry.STATES.WALK_BANK;
    }

    return Fxjewellry.STATES.IDLE;
  }

  public int loop() {
    this.state = getState();
    int i;
    int m;
    int k;
    switch (Fxjewellry.1.$SwitchMap$Fxjewellry$STATES[this.state.ordinal()]) {
    case 1:
      this.bank.close();
      for (i = 0; (i < 20) && (this.bank.isOpen()); i++)
        sleep(random(100, 200));
      sleep(500);

      break;
    case 2:
      this.bank.open();
      for (i = 0; (i < 10) && (!this.bank.isOpen()); i++) {
        sleep(random(100, 200));
      }
      break;
    case 3:
      this.interfaces.clickContinue();

      break;
    case 4:
      RSObject localRSObject = this.objects.getNearest(Fxjewellry.constants.furnace);
      if (ifaceIsUp(this.interfaces.getComponent(446, 0)))
        break;
      if (!this.inventory.isItemSelected())
        this.inventory.getItem(new int[] { 2357 }).doAction("Use");
      if (ifaceIsUp(this.interfaces.getComponent(446, 0)))
        break;
      this.camera.turnToObject(localRSObject);
      while (this.players.getMyPlayer().isMoving())
        sleep(random(100, 200));
      for (m = 0; (m < 10) && (!this.inventory.isItemSelected()); m++)
        sleep(random(100, 200));
      if (ifaceIsUp(this.interfaces.getComponent(446, 0)))
        break;
      if ((localRSObject != null) && (this.inventory.isItemSelected())) {
        localRSObject.doHover();
        log("Left-clicking furnace");
        this.mouse.click(false);
        for (m = 0; (m < 10) && (!this.menu.isOpen()); m++)
          sleep(random(100, 200));
        log("Going through the menu options");
        m = 0;
        for (String str : this.menu.getItems()) {
          log(str);
          if ((str.contains("Gold bar")) && (str.contains("Furnace")))
            break;
          m++;
        }
        if (m < this.menu.getItems().length)
          this.menu.clickIndex(m);
        else {
          this.mouse.move(random(0, 10), random(0, 10));
        }
      }
      m = 0;
      while ((m < 10) && (!ifaceIsUp(this.interfaces.getComponent(446, 0))))
      {
        sleep(random(100, 200));

        m++;
      }

      break;
    case 5:
      try
      {
        int j = this.inventory.getCount();
        this.bank.depositAllExcept(Fxjewellry.constants.moulds.moulds);
        for (m = 0; (m < 10) && (this.inventory.getCount() == j); m++)
          sleep(random(100, 200));
      } catch (Exception localException) {
        localException.printStackTrace();
      }

    case 6:
      RSComponent localRSComponent = this.interfaces.getComponent(446, this.ifaceID);

      if (!ifaceIsUp(localRSComponent))
        break;
      localRSComponent.doHover();
      this.mouse.click(false);
      for (m = 0; (m < 10) && (!this.menu.isOpen()); m++)
        sleep(random(100, 200));
      if (!this.menu.isOpen())
        break;
      this.menu.doAction("Make All");
      this.utilThread.reset(0L);
      m = 0;

      for (; (m < 10) && (ifaceIsUp(this.interfaces.getComponent(446, 0))) && (this.players.getMyPlayer().getAnimation() == -1); m++) {
        sleep(random(100, 200));
      }
      break;
    case 7:
      this.running = false;

      return -1;
    case 8:
      this.walking.setRun(true);
      for (k = 0; (k < 10) && (!this.walking.isRunEnabled()); k++) {
        sleep(random(100, 200));
      }
      break;
    case 9:
      this.smelter.walkToBank();

      break;
    case 10:
      this.smelter.walkToFurnace();

      break;
    case 11:
      if (this.secondaryID == -1) {
        withdrawBankItem(2357, 0);
        for (k = 0; (k < 15) && (!this.inventory.contains(2357)); k++)
          sleep(random(100, 200));
      } else {
        withdrawBankItem(2357, 13);
        for (k = 0; (k < 15) && (!this.inventory.contains(2357)); k++)
          sleep(random(100, 200));
        if (this.inventory.contains(this.secondaryID)) break;
        withdrawBankItem(this.secondaryID, 13);
        for (k = 0; (k < 15) && (!this.inventory.contains(this.secondaryID)); k++)
          sleep(random(100, 200));
      }
      break;
    case 12:
    }

    return 100;
  }

  public void onFinish() {
    log("Final status was " + this.state.getDesc());
    this.running = false;
  }

  public void onRepaint(Graphics paramGraphics) {
    this.expGained = (this.skills.getCurrentExp(12) - this.expStart);
    this.levelGained = (this.skills.getCurrentLevel(12) - this.levelStart);

    RenderingHints localRenderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Color localColor1 = new Color(0, 0, 255, 108);
    Color localColor2 = new Color(0, 0, 0);
    Color localColor3 = new Color(255, 255, 255);

    BasicStroke localBasicStroke = new BasicStroke(1.0F);

    Font localFont1 = new Font("Calibri", 0, 15);
    Font localFont2 = new Font("Calibri", 0, 9);
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    localGraphics2D.setRenderingHints(localRenderingHints);

    localGraphics2D.setColor(localColor1);
    localGraphics2D.fillRect(37, 35, 235, 124);
    localGraphics2D.setColor(localColor2);
    localGraphics2D.setStroke(localBasicStroke);
    localGraphics2D.drawRect(37, 35, 235, 124);
    localGraphics2D.setFont(localFont1);
    localGraphics2D.setColor(localColor3);
    localGraphics2D.drawString("Fxjewellry by NoEffex", 74, 55);
    localGraphics2D.setFont(localFont2);
    localGraphics2D.drawString("Time Running", 74, 72);
    localGraphics2D.drawString(this.timekeeper.getRuntimeString(), 200, 72);
    localGraphics2D.drawString("Idle Time", 74, 84);
    localGraphics2D.drawString(Long.toString(this.utilThread.getIdle()), 200, 84);
    localGraphics2D.drawString("Items Done", 74, 96);
    localGraphics2D.drawString(Integer.toString(this.numDone), 200, 96);
    localGraphics2D.drawString("Items/Hr", 74, 108);
    localGraphics2D.drawString(Long.toString(this.timekeeper.calcPerHour(this.numDone)), 200, 108);
    localGraphics2D.drawString("Experience Gained", 74, 120);
    localGraphics2D.drawString(Integer.toString(this.expGained), 200, 120);
    localGraphics2D.drawString("Experience/Hr", 74, 132);
    localGraphics2D.drawString(Long.toString(this.timekeeper.calcPerHour(this.expGained)), 200, 132);
    localGraphics2D.drawString("Levels Gained", 74, 144);
    localGraphics2D.drawString(Integer.toString(this.levelGained), 200, 144);
  }

  public boolean onStart()
  {
    Fxjewellry.SettingsManager localSettingsManager = new Fxjewellry.SettingsManager(this, GlobalConfiguration.Paths.getSettingsDirectory() + File.separator + "Fxjewellry.properties");

    this.gui = new Fxjewellry.GUI(this, localSettingsManager);
    this.gui.setVisible(true);
    while (this.gui.isVisible()) {
      sleep(100);
    }
    localSettingsManager.save();
    if (this.gui.isCancelled() == true) {
      return false;
    }
    int i = this.gui.getGoldLocation();
    switch (i) {
    case 0:
      this.smelter = new Fxjewellry.AlKaharid(this);
      break;
    case 1:
      this.smelter = new Fxjewellry.Edgeville(this);
      break;
    default:
      return false;
    }
    int j = this.gui.getJewel();
    String str = this.gui.getGoldType();
    this.secondaryID = Fxjewellry.constants.gems.combo[j];
    if (str.equals("Bracelet"))
      this.ifaceID = Fxjewellry.constants.ifaces.bracelets.combo[j];
    else if (str.equals("Amulet"))
      this.ifaceID = Fxjewellry.constants.ifaces.amulets.combo[j];
    else if (str.equals("Necklace"))
      this.ifaceID = Fxjewellry.constants.ifaces.necklaces.combo[j];
    else if (str.equals("Ring"))
      this.ifaceID = Fxjewellry.constants.ifaces.rings.combo[j];
    this.mouseHi = this.gui.getMouseHi();
    this.mouseLo = this.gui.getMouseLo();
    this.utilThread = new Fxjewellry.UtilThread(this);
    new Fxjewellry.AntiBan(this);
    this.mouse.setSpeed(2);
    this.expStart = this.skills.getCurrentExp(12);
    this.levelStart = this.skills.getCurrentLevel(12);
    return true;
  }

  private boolean ifaceIsUp(RSComponent paramRSComponent)
  {
    return (paramRSComponent != null) && (paramRSComponent.isValid()) && (paramRSComponent.getLocation().x > 0) && (paramRSComponent.getLocation().y > 0);
  }

  public boolean withdrawBankItem(int paramInt1, int paramInt2)
  {
    if (!this.bank.isOpen()) {
      return false;
    }
    RSItem localRSItem = this.bank.getItem(paramInt1);
    if ((localRSItem != null) && (localRSItem.getID() != paramInt1))
      return false;
    for (int i = 0; (i < 40) && (localRSItem.getID() != paramInt1); i++) {
      localRSItem = this.bank.getItem(paramInt1);
      sleep(random(100, 200));
    }
    if ((localRSItem == null) || (!localRSItem.getComponent().isValid()) || (localRSItem.getID() != paramInt1))
    {
      return false;
    }
    localRSItem.getComponent().doHover();
    this.mouse.click(false);
    sleep(random(200, 300));
    if ((paramInt2 == 0) || (paramInt2 >= 28 - this.inventory.getCount())) {
      this.menu.doAction("Withdraw-All");
    } else if (paramInt2 == -1) {
      this.menu.doAction("Withdraw-All but one");
    } else {
      for (i = 0; (i < 40) && (!this.menu.isOpen()); i++)
        sleep(random(100, 200));
      if (!this.menu.contains("Withdraw-" + Integer.toString(paramInt2))) {
        this.menu.doAction("Withdraw-X");
        i = 0;
        for (; (i < 40) && (!this.interfaces.getComponent(752, 4).isValid()); i++) {
          sleep(random(100, 200));
        }
        sleep(random(1000, 1500));
        if ((this.interfaces.getComponent(752, 4).getText().contains("amount")) || (!this.interfaces.getComponent(752, 4).getText().contains("purchase")))
        {
          this.keyboard.sendText(Integer.toString(paramInt2), true);
          sleep(2000);
        }
      } else {
        for (i = 0; (i < 40) && (!this.menu.isOpen()); i++)
          sleep(random(100, 200));
        this.menu.doAction("Withdraw-" + Integer.toString(paramInt2));
      }
    }
    return true;
  }
}